package com.baixiong.springmvc.controller;


import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.util.UUID;

@Controller
public class FileController {

    @RequestMapping(value = "/fileup",method = RequestMethod.POST)
    //multipartFile就代表从客户端传来的文件
    public String fileup(@RequestParam("fileName") MultipartFile multipartFile , HttpServletRequest request) throws IOException {
        //获取请请求参数的名字
        String name = multipartFile.getName();
        System.out.println(name);
        //获取的是文件真实名字
        String originalFilename = multipartFile.getOriginalFilename();
        System.out.println(originalFilename);

        //读取客户端传来的数据，写道服务器上
        //输入流
        InputStream inputStream = multipartFile.getInputStream();//获取输入流，负责读客户端的文件
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);//封装成带有缓冲区的输入流

        //输出流
        ServletContext application = request.getServletContext();
        String realPath = application.getRealPath("/upload");
        File file =new File(realPath);
        if (!file.exists()){
            file.mkdirs();
        }
        File destFile = new File(file.getAbsolutePath()+"/"+ UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf(".")));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFile));

        //一边读一边写
        byte[] bytes = new byte[1024*10];

        int readCount = 0;
        while ((readCount = bufferedInputStream.read(bytes)) !=-1){
            bufferedOutputStream.write(bytes,0,readCount);
        }
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        bufferedInputStream.close();


        return "OK";

    }

    @GetMapping("/download")
    public ResponseEntity<byte[]> downloadFile( HttpServletRequest request)throws IOException{


        File file = new File(request.getServletContext().getRealPath("/upload") + "/.20240226142213.jpg");
    //    创建响应头
        HttpHeaders headers =new HttpHeaders();
    //    设置响应内容类型
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    //    设置下载文件的名称
        headers.setContentDispositionFormData("attachment",file.getName());

    //    下载文件
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(Files.readAllBytes(file.toPath()),headers, HttpStatus.OK);
        return entity;



    }

}
